//jshint ignore:start
import DBTransaction from "./DBTransaction.js";
export default class DBConnection {
	constructor(name, version) {
		this.name = name; //数据库名称
		this.version = version; //数据库版本
	}
	/**
	 * 创建数据库连接
	 * @param setter
	 * @returns { Promise<IDBDatabase> }
	 */
	async getConnection(setter) {
		const connect = indexedDB.open(this.name, this.version); //打开数据库
		connect.onupgradeneeded = ({ target }) => setter(target.result); //设置数据库更新回调
		return new Promise((resolve, reject) => {
			//打开数据库成功
			connect.onsuccess = ({ target }) => {
				this.connect = target.result;
				resolve(target.result); //返回数据库对象
			};
			//打开数据库失败
			connect.onerror = reject;
		});
	}
	/**
	 * 新建事务
	 * @param flag
	 * @returns { DBTransaction }
	 */
	createTransaction(flag) {
		return new DBTransaction(this.connect, flag);
	}
}
